#!/bin/csh
#...........................................................................
#BSUB -a poe                    # use LSF poe elim
##BSUB -x                        # exclusive use of node (not_shared)
#BSUB -n 1                      # total tasks needed
#BSUB -R "span[ptile=16]"       # max number of tasks (MPI) per node
#BSUB -J NDGV1
#BSUB -o %J.runout              # output filename
#BSUB -e %J.runout              # error filename
#BSUB -W 05:59
##BSUB -P P93300642
#BSUB -P WYOM0024
##BSUB -q regular             # queue
##BSUB -q small                  # regular 
#BSUB -q caldera

set echo 

# CAM5 orography 
# 
# /glade/p/cesm/cseg/inputdata/atm/cam/topo/USGS-gtopo30_1.9x2.5_remap_c050602.nc oro_ecmwf_T170.nc
#  
# cdo sp2gp -sp2sp,170 -gp2sp -remapcon,t170grid oro_cam5.nc oro_cam5_t170.nc


foreach yy (2010) 
foreach mm (01 02 03 04 05 06 07 08 09 10 11 12) 
###foreach mm (01) 
foreach dd (01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31) 

setenv mpath /glade/p/rda/data/ds627.0/ei.oper.an.ml/${yy}${mm}
setenv tpath /glade/scratch/kaizhang/data/ndata/tmpv1
setenv opath /glade/scratch/kaizhang/data/ndata/output
setenv ipath /glade/scratch/kaizhang/data/ndata/input
setenv spath /glade/scratch/kaizhang/data/ndata/cam5nud

mkdir -p ${tpath} 

foreach hh (00 06 12 18) 

echo ''
echo ''
echo ${yy}${mm}${dd}${hh} 
echo ''
echo ''

rm -f ${tpath}/ei.oper.an.ml.regn128sc.${yy}${mm}${dd}${hh}.grb
rm -f ${tpath}/ei.oper.an.ml.regn128uv.${yy}${mm}${dd}${hh}.grb

ln -s ${mpath}/ei.oper.an.ml.regn128sc.${yy}${mm}${dd}${hh} ${tpath}/ei.oper.an.ml.regn128sc.${yy}${mm}${dd}${hh}.grb
ln -s ${mpath}/ei.oper.an.ml.regn128uv.${yy}${mm}${dd}${hh} ${tpath}/ei.oper.an.ml.regn128uv.${yy}${mm}${dd}${hh}.grb

#.............................................................................. 
# 3. Do vertical interpolation using cdo remapeta 
#.............................................................................. 

#       pold(k,:,:) = ( surp(itim,:,:)/100.) * (hybi_cam(k)) + 1013.25 * (hyai_cam(k))
#
#       pold(k,:,:) = ( surp(itim,:,:)/100.) * (hybi_echam(k)) + (hyai_echam(k)) / 100  
#
 rm -f ${tpath}/tmp${yy}${mm}${dd}${hh}.grb 

 cdo merge ${tpath}/ei.oper.an.ml.regn128sc.${yy}${mm}${dd}${hh}.grb ${tpath}/ei.oper.an.ml.regn128uv.${yy}${mm}${dd}${hh}.grb ${tpath}/tmp${yy}${mm}${dd}${hh}.grb 

 rm -f ${tpath}/tmp${yy}${mm}${dd}${hh}_stps.nc 
 rm -f ${tpath}/tmp${yy}${mm}${dd}${hh}_uuvv.nc 

 cdo remapeta,vct_L30,oro_cam5_t170.nc  ${tpath}/tmp${yy}${mm}${dd}${hh}.grb ${tpath}/tmp${yy}${mm}${dd}${hh}_stps.nc 
 cdo remapeta,vct_L30,oro_cam5_t170.nc  ${tpath}/tmp${yy}${mm}${dd}${hh}.grb ${tpath}/tmp${yy}${mm}${dd}${hh}_uuvv.nc

 rm -f ${tpath}/tmp${yy}${mm}${dd}${hh}_o1.nc 
 rm -f ${tpath}/tmp${yy}${mm}${dd}${hh}_o2.nc 

 cdo remapcon,grid_2x2 -expr,'var152=exp(var152);var130=var130;var156=var129/9.8;' -selvar,var130,var152,var129 ${tpath}/tmp${yy}${mm}${dd}${hh}_stps.nc ${tpath}/tmp${yy}${mm}${dd}${hh}_o1.nc 
 cdo remapcon,grid_2x2 -selvar,var131,var132 ${tpath}/tmp${yy}${mm}${dd}${hh}_uuvv.nc ${tpath}/tmp${yy}${mm}${dd}${hh}_o2.nc 

 rm -f ${opath}/ndg_v1_2x2.5_${yy}${mm}${dd}${hh}.nc 

 cdo merge ${tpath}/tmp${yy}${mm}${dd}${hh}_o1.nc ${tpath}/tmp${yy}${mm}${dd}${hh}_o2.nc ${opath}/ndg_v1_2x2.5_${yy}${mm}${dd}${hh}.nc 

#.............................................................................. 
# 3. Do vertical interpolation using cdo remapeta 
#.............................................................................. 

cat > loadit_z3 << EOF
 fna = "${opath}/ndg_v1_2x2.5_${yy}${mm}${dd}${hh}.nc" 
 fno = "${opath}/z3_2x2.5_${yy}${mm}${dd}${hh}.nc"
EOF

 ncl ${spath}/make_z3.ncl

#
# cdo remapcon,grid_2x2 ${tpath}/tmp${yy}${mm}${dd}${hh}_z3.nc ${opath}/z3_2x2.5_${yy}${mm}${dd}${hh}.nc
#

end # end hh 

end # end dd 

##cdo mergetime ${opath}/ndg_v1_2x2.5_${yy}${mm}????.nc ${opath}/ndg_v1_2x2.5_${yy}${mm}.nc
##rm -f         ${opath}/ndg_v1_2x2.5_${yy}${mm}????.nc
##
##cdo mergetime ${opath}/z3_2x2.5_${yy}${mm}????.nc ${opath}/z3_2x2.5_${yy}${mm}.nc
##rm -f         ${opath}/z3_2x2.5_${yy}${mm}????.nc


end # end mm

end # end yy




